package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dto.ServiceCategoryDTO;
import com.eastfair.venueservice.entity.ServiceCategory;
import com.eastfair.venueservice.dto.ServiceCategorySaveDTO;
import com.eastfair.venueservice.dto.ServiceCategoryUpdateDTO;
import com.eastfair.venueservice.dto.ServiceCategoryPageQuery;
import com.eastfair.venueservice.service.ServiceCategoryService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.venueservice.vo.ServiceCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 服务分类
 * </p>
 *
 * @author lyw
 * @date 2022-07-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceCategory")
@Api(value = "ServiceCategory", tags = "服务分类")
@PreAuth(replace = "venueservice:serviceCategory:")
public class ServiceCategoryController extends SuperController<ServiceCategoryService, Long, ServiceCategory, ServiceCategoryPageQuery, ServiceCategorySaveDTO, ServiceCategoryUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ServiceCategoryService serviceCategoryService;

    @Override
    public void handlerResult(IPage<ServiceCategory> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ServiceCategory> serviceCategoryList = list.stream().map((map) -> {
            ServiceCategory serviceCategory = ServiceCategory.builder().build();
            //TODO 请在这里完成转换
            return serviceCategory;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceCategoryList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceCategoryPageQuery> params) {
        ServiceCategoryPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * 查询服务分类列表分页
     * @param params
     * @return
     */
    @ApiOperation(value = "查询服务分类列表")
    @PostMapping(value = "/queryServiceCategoryList")
    public R<Page<ServiceCategoryVO>> queryServiceCategoryList(@RequestBody PageParams<ServiceCategoryPageQuery> params) {
        try {
            Page<ServiceCategoryVO> page = serviceCategoryService.queryServiceCategoryList(params);
            return R.success(page);
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询服务分类列表
     * @param categoryType
     * @return
     */
    @ApiOperation(value = "查询服务分类列表")
    @GetMapping(value = "/getCategoryList/{categoryType}/{exhibitionManageId}")
    public R<List<ServiceCategoryVO>> getCategoryListByExhibitionManageId(@PathVariable("categoryType") Integer categoryType,@PathVariable("exhibitionManageId") Long exhibitionManageId){
        try {
            List<ServiceCategoryVO> serviceCategoryVOList= serviceCategoryService.getCategoryListByExhibitionManageId(categoryType,exhibitionManageId);
            return R.success(serviceCategoryVOList);
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 查询服务分类列表
     * @param categoryType
     * @return
     */
    @ApiOperation(value = "查询服务分类列表")
    @GetMapping(value = "/getCategoryList/{categoryType}")
    public R<List<ServiceCategoryVO>> getCategoryList(@PathVariable("categoryType") Integer categoryType){
        try {
            List<ServiceCategoryVO> serviceCategoryVOList= serviceCategoryService.getCategoryList(categoryType);
            return R.success(serviceCategoryVOList);
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }


    /**
     * 保存服务分类
     * @param serviceCategoryDTO
     * @return
     */
    @ApiOperation(value = "保存服务分类")
    @SysLog("添加服务分类")
    @PostMapping(value = "/saveServiceCategoryInfo")
    public R<Boolean> saveServiceCategoryInfo(@RequestBody @Validated(ServiceCategoryDTO.Save.class) ServiceCategoryDTO serviceCategoryDTO){
        try {
            return R.success(serviceCategoryService.saveServiceCategoryInfo(serviceCategoryDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }





    /**
     * 修改服务分类
     * @param serviceCategoryDTO
     * @return
     */
    @ApiOperation(value = "修改服务分类")
    @SysLog("修改服务分类")
    @PostMapping(value = "/updateServiceCategoryById")
    public R<Boolean> updateServiceCategoryById(@RequestBody @Validated(ServiceCategoryDTO.Update.class)  ServiceCategoryDTO serviceCategoryDTO){
        try {
            return R.success(serviceCategoryService.updateServiceCategoryById(serviceCategoryDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }


    }

    /**
     * 删除服务分类
     * @param id
     * @return
     */
    @ApiOperation(value = "删除服务分类")
    @SysLog("删除服务分类")
    @GetMapping(value = "/delServiceCategoryById/{id}")
    public R<Boolean> delServiceCategoryById(@PathVariable("id") Long id){
        try {
            return R.success(serviceCategoryService.delServiceCategoryById(id));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }


    /**
     * 查询服务分类列表
     * @param ids ids
     * @return R<List<ServiceCategoryVO>>
     */
    @PostMapping("/getCategoryListByIds")
    public R<List<ServiceCategoryVO>> getCategoryListByIds(@RequestBody List<Long> ids){
        return R.success(baseService.getCategoryListByIds(ids));
    }

}
